package com.zkk.web;

import com.alibaba.fastjson.JSON;
import com.zkk.pojo.Card;
import com.zkk.pojo.DailyRecord;
import com.zkk.service.AccountService;
import com.zkk.service.CardService;
import com.zkk.service.DailyRecordService;
import com.zkk.service.impl.AccountServiceImpl;
import com.zkk.service.impl.CardServiceImpl;
import com.zkk.service.impl.DailyRecordServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;
@WebServlet("/rechargingMoney")
public class rechargingMoneyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String balance = req.getParameter("balance");
        BigDecimal bigDecimal = new BigDecimal(balance);
        //上面得到要充值的金额
        String id_card = req.getParameter("id_card");
        Card card = new Card();
        card.setId_card(id_card);
       /* 这个balance值不应该是直接被拿来用的， 而是应该是要加在原来的余额上的
        需要先调用通过id查询余额的方法，得到的余额，然后和前端传过来的值进行相加，再把总的余额更新到数据库中*/
        AccountService accountService = new AccountServiceImpl();
        BigDecimal property = accountService.queryAccount().getProperty();
        BigDecimal sumProperty = property.add(bigDecimal);
        //上面就查询到了要更新的余额
        String id_number = accountService.queryAccount().getId_number();
        //上面得到要被更新的账号身份证号


        //获得支付密码
        String payment_pwd = req.getParameter("payment_pwd");

        //添加日志记录
        DailyRecord dailyRecord = new DailyRecord();
        dailyRecord.setOutPutCard_Id(id_card);
        dailyRecord.setInPutCard_Id(id_number);
        dailyRecord.setDate(new Date());
        dailyRecord.setAction("充值账号");
        dailyRecord.setMoney(bigDecimal);



        CardService cardService = new CardServiceImpl();
        BigDecimal money = cardService.queryMoney(card);
        //上面查询到卡里的余额
        BigDecimal subtract = money.subtract(bigDecimal);

        String code = req.getParameter("code");
        String attribute =(String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        //创建个为0的BigDecimal
        BigDecimal bigDecimal1 = new BigDecimal(0);

        if(payment_pwd.equals("123456")){
            if(code.equals(attribute) && bigDecimal.compareTo(bigDecimal1)==1 && money.compareTo(bigDecimal)==1){
           /* 调用更新方法，更新充值后的银行卡余额
            充值的金额大于0,还要卡里的余额大于将要充值支付的金额
            下面得到被减后的金额到数据库*/
                //从银行卡转出钱
                cardService.rechargingMoney(id_card, subtract);
                //收到钱到账号
                int i = accountService.updateProperty(id_number, sumProperty);
                if(i==1){
                    DailyRecordService dailyRecordService = new DailyRecordServiceImpl();
                    dailyRecordService.record(dailyRecord);
                    String s = JSON.toJSONString(1);
                    resp.getWriter().write(s);
                }
            }else {
                String s = JSON.toJSONString(2);
                resp.getWriter().write(s);
            }
        }else {
            String s = JSON.toJSONString(0);
            resp.getWriter().write(s);
        }

    }
}
